package com.itheima.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.interceptor.AdminHolder;
import com.itheima.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
public class AdminController {


    @Autowired
    AdminManager adminManager;


    //生成验证码图片
    @GetMapping("/system/users/verification")
    public void getVerification(String uuid, HttpServletResponse response) throws IOException {
        //调用manager生成验证码图片
        LineCaptcha lineCaptcha = adminManager.getVerification(uuid);
        //返回验证码图片
        lineCaptcha.write(response.getOutputStream());
    }

    //用户登录
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String,String> map){
        //1.接受4个参数
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");

        //2.调用manager完成登陆操作
        return adminManager.login(username,password,verificationCode,uuid);
    }

    // 获取用户信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile() {
        return  ResponseEntity.ok(AdminHolder.get());
    }

    // 用户退出
    @PostMapping("/system/users/logout")
    public void logout(@RequestHeader("Authorization") String token){
        // 调用manager
        adminManager.logout(token);
    }


    /*
    222222222222222222222222222222222222222222222222222222222222222222222222222
     */

    //生成验证码图片
    /*@GetMapping("/system/users/verification")
    public void getVerification1(String uuid, HttpServletResponse response) throws IOException {
        //调用manager生成验证码图片
        LineCaptcha lineCaptcha = adminManager.getVerification(uuid);

        //返回验证码图片
        lineCaptcha.write(response.getOutputStream());
    }*/

    //用户登录
    /*@PostMapping("/system/users/login")
    public ResponseEntity login1(@RequestBody Map<String,String> map){
        //1.接受4个参数
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");

        //2.调用manager完成登陆操作
        return adminManager.login(username,password,verificationCode,uuid);
    }*/





}
